package com.guigu.www.denghao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.denghao.service.DHHealthManagerService;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DHHealthManagerServiceImpl implements DHHealthManagerService {

    @Autowired
    private HealthManagerMapper healthManagerMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ConstitutionTestMapper constitutionTestMapper;
    @Autowired
    private MindTestMapper mindTestMapper;
    @Autowired
    private RiskTestMapper riskTestMapper;

    @Override
    public PageVo<HealthManager> findLikeUser(Integer pageNum, Integer pageSize, String hmName, String hmAge, String hmPhone) {
        IPage<HealthManager> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<HealthManager> wrapper = new QueryWrapper<>();
        if (!hmAge.equals("")){
            wrapper.eq("hm_age",hmAge);
        }
        if (!hmName.equals("")){
            wrapper.like("hm_name",hmName);
        }
        if (!hmPhone.equals("")){
            wrapper.like("hm_phone",hmPhone);
        }
        iPage = healthManagerMapper.selectPage(iPage,wrapper);
        return new PageVo<>(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public Integer addUser(HealthManager healthManager) {
        // 获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        // 从身份证上获取出生日期和年龄
        // 出生日期
        String birthday = healthManager.getHmIdentity().substring(6, 10) + "-"
                + healthManager.getHmIdentity().substring(10, 12) + "-"
                + healthManager.getHmIdentity().substring(12, 14);
        //年龄
        int year = Calendar.getInstance().get(Calendar.YEAR);
        Integer age = (year - Integer.parseInt(healthManager.getHmIdentity().substring(6, 10)));
        healthManager.setHmAge(age);
        healthManager.setHmCreateDate(time);
        healthManager.setHmBirthday(birthday);
        healthManager.setHmState(0);
        healthManager.setHmImg("jiangkang.png");
        return healthManagerMapper.insert(healthManager);
    }

    @Override
    public Integer upUser(HealthManager healthManager) {
        // 从身份证上获取出生日期和年龄
        // 出生日期
        String birthday = healthManager.getHmIdentity().substring(6, 10) + "-"
                + healthManager.getHmIdentity().substring(10, 12) + "-"
                + healthManager.getHmIdentity().substring(12, 14);
        //年龄
        int year = Calendar.getInstance().get(Calendar.YEAR);
        Integer age = (year - Integer.parseInt(healthManager.getHmIdentity().substring(6, 10)));
        healthManager.setHmAge(age);
        healthManager.setHmBirthday(birthday);
        return healthManagerMapper.updateById(healthManager);
    }

    @Override
    public Integer delUser(Integer hmId, Integer hmState) {
        HealthManager healthManager = new HealthManager();
        healthManager.setHmId(hmId);
        healthManager.setHmState(hmState);
        return healthManagerMapper.updateById(healthManager);
    }

    @Override
    public HealthManager queryByIdHealth(Integer hmId) {
        return healthManagerMapper.selectById(hmId);
    }

    @Override
    public void upTouXiang(String newFileName, Integer hmId) {
        HealthManager healthManager = new HealthManager();
        healthManager.setHmImg(newFileName);
        healthManager.setHmId(hmId);
        healthManagerMapper.updateById(healthManager);
    }

    @Override
    public HealthManager referenceUserInfo(Integer hmId) {
        HealthManager healthManager = healthManagerMapper.selectById(hmId);
        Role role = roleMapper.selectById(healthManager.getRoleId());
        healthManager.setRole(role);
        return healthManager;
    }

    @Override
    public PageVo<HealthManager> findLikeUser1(Integer pageNum, Integer pageSize, String hmName, String hmDisplayState, String hmPhone) {
        IPage<HealthManager> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<HealthManager> wrapper = new QueryWrapper<>();
        if (!hmDisplayState.equals("")){
            wrapper.eq("hm_display_state",hmDisplayState);
        }
        if (!hmName.equals("")){
            wrapper.like("hm_name",hmName);
        }
        if (!hmPhone.equals("")){
            wrapper.like("hm_phone",hmPhone);
        }
        wrapper.eq("hm_state",0);
        wrapper.eq("role_id",2);
        iPage = healthManagerMapper.selectPage(iPage,wrapper);
        return new PageVo<>(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public Integer delDisplayUser(Integer hmId, Integer hmDisplayState) {
        HealthManager healthManager = new HealthManager();
        healthManager.setHmId(hmId);
        healthManager.setHmDisplayState(hmDisplayState);
        return healthManagerMapper.updateById(healthManager);
    }

    @Override
    public List<HealthManager> queryDisplayOrder() {
        QueryWrapper<HealthManager> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",2);
        wrapper.eq("hm_display_state",0);
        wrapper.orderByAsc("hm_display_order");
        return healthManagerMapper.selectList(wrapper);
    }

    @Override
    public void upDisplayOrder(Integer[] hmIds) {
        for (int i = 0; i < hmIds.length; i++) {
            HealthManager healthManager = new HealthManager();
            healthManager.setHmId(hmIds[i]);
            healthManager.setHmDisplayOrder(i);
            healthManagerMapper.updateById(healthManager);
        }
    }

    @Override
    public ConstitutionTest queryByMemIdConstitutionTest(Integer memId) {
        QueryWrapper<ConstitutionTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",memId);
        return constitutionTestMapper.selectOne(wrapper);
    }

    @Override
    public MindTest queryByMemIdMindTest(Integer memId) {
        QueryWrapper<MindTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",memId);
        return mindTestMapper.selectOne(wrapper);
    }

    @Override
    public RiskTest queryByMemIdRiskTest(Integer memId) {
        QueryWrapper<RiskTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",memId);
        return riskTestMapper.selectOne(wrapper);
    }
}
